# -*- coding:utf-8 -*-

import pygame
import random
import math
import copy
from pygame.locals import *

width = 1200
height = 800

radius = 15
march_step = 2

start_pos = [10, 10]
end_pos = [200, 200]


class GapWall:
    def __init__(self, x, gap):
        """
        init the march
        """
        self.x = x
        self.gap = gap   # 缺口中心位置y坐标

# s = 1/ 2 * g *t^2

total_count = 0


def get_new_pos(run_count):
    return [start_pos[0] + march_step * total_count, int(round(start_pos[1] + float(run_count * run_count) / 20))]


wall_distance = 300
wall_width = 20
wall_gap = 300


# 每隔一段距离生成固定宽度缺口的墙壁
def gen_wall():
    wall_list = []
    for x in range(wall_distance, width, wall_distance):
        gapwall = GapWall(x, random.randint(0 + wall_gap / 2, height - wall_gap / 2))
        wall_list.append(gapwall)
    return wall_list


def main():
    pygame.init()
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode([width, height])

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))

    restart = True
    while restart:

        global total_count

        screentitle = pygame.display.set_caption("free fall")
        player_pos = start_pos

        run_count = 0
        total_count = 0

        wall_list = gen_wall()

        wall_rect_list = []

        running = True
        while running:
            # fill the background is white
            screen.fill([255, 255, 255])

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
                elif event.type == MOUSEBUTTONDOWN:
                    run_count = -run_count

            time_pass = clock.tick(30)

            #  绘制墙壁
            for wall in wall_list:
                wall_rect_up = pygame.draw.rect(screen, (0, 255, 0), ((wall.x - wall_width / 2, 0), (wall_width, wall.gap - wall_gap/ 2)))
                wall_rect_down = pygame.draw.rect(screen, (0, 255, 0), ((wall.x - wall_width / 2, wall.gap + wall_gap / 2), (wall_width, height - wall.gap - wall_gap / 2)))
                wall_rect_list.append(wall_rect_up)
                wall_rect_list.append(wall_rect_down)

            new_pos = get_new_pos(run_count)
            run_count = run_count + 1
            total_count = total_count + 1

            new_rect = pygame.draw.circle(screen, [0, 0, 255], new_pos, radius, 0)

            for wall_rect in wall_rect_list:
                if wall_rect.colliderect(new_rect):
                    running = False

            # if new_rect.colliderect(end_pos):
            if run_count > 10000:
                running = False
            pygame.display.update()
        # game over background
        pygame.font.init()
        screen.fill([100, 0, 0])
        font = pygame.font.Font(None, 48)
        text = font.render("Game Over !!!", True, (255, 0, 0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery + 24
        screen.blit(text, textRect)
        # keydown r restart,keydown n exit
        while 1:
            event = pygame.event.poll()
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    restart = True
                    break
                if event.key == pygame.K_n:
                    restart = False
                    break
            pygame.display.update()


if __name__ == '__main__':
    main()
